/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package anton.stock;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

/**
 *
 * @author johan
 */
@ManagedBean(name="destroyStock")
@SessionScoped

public class DestroyStock implements Serializable {
    
   @Resource(name="jdbc/antonsDatasource") // data source to use for mysql queries
    
   private DataSource ds; 
   
   int stat;
   
     public String destroy(int itemId ) throws SQLException
   {    
   
        String sql;// variable for sql statements
        String target = "/admin/stock/liststock?faces-redirect=true";// link
        // checks connection
        try (Connection con = ds.getConnection()) {
            sql="delete from DinnerIngredients where item_ID ="+"'"+itemId+"'";
            
            
            
            PreparedStatement ps; // query delete items
            ps = con.prepareStatement(sql );
            
            stat= ps.executeUpdate();
            sql="delete from Stock where item_ID ="+"'"+itemId+"'";// deletes entry with specific item id
            
            ps = con.prepareStatement(sql );
            
            stat = ps.executeUpdate();// stat gets a diffrent value depending on the action
            
            // removes deleted items from list
        }
        
        return target;
   
   }
    
    
    
    
}
